package com.yzjs.localhost.service.hrmRsLeave.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmPositionChange.HrmPositionChangeEntity;
import com.yzjs.localhost.entity.hrmRsLeave.HrmRsLeaveEntity;
import com.yzjs.localhost.dao.hrmRsLeave.HrmRsLeaveMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmRsLeave.HrmRsLeaveService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("hrmRsLeaveService")
public class HrmRsLeaveServiceImpl extends BaseServiceImpl<HrmRsLeaveEntity, HrmRsLeaveMapper > implements HrmRsLeaveService {

    @Override
    public ComparisonResultDTO hrmRsLeaveCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce =dto.getBeforce();
        String beforceInfo  = beforce.getInfo();
        Map<String,Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo  = after.getInfo();

        ComparisonResultDTO resultDTO = null ;
        if (beforce.getOperatorCode().contains("add")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"muserId,deptId,remark,leaveExplain,createTime,leaveId,startTime,endTime,applyTime,leaveStatus");
        }else if (beforce.getOperatorCode().contains("edit")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"muserId,deptId,remark,updateTime,delFlag,leaveExplain,createTime,leaveId,startTime,endTime,applyTime,leaveStatus");
        } else if (beforce.getOperatorCode().contains("remove")){
            beforceParse.put("delFlag","2");
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"leaveIds,delFlag");
            if (null==afterParse.get("leaveIds")){
                resultDTO.setAbstractName("请假ID为空");
                resultDTO.setWarningInfo("请假ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmRsLeaveEntity hrmRsLeaveEntity = new HrmRsLeaveEntity();
            hrmRsLeaveEntity.setLeaveId(Long.valueOf((String) afterParse.get("leaveIds")));
            hrmRsLeaveEntity.setDelFlag((String) afterParse.get("delFlag"));
            hrmRsLeaveEntity.setDataIdentification(after.getDataIdentification());
            this.insert(hrmRsLeaveEntity);
            return resultDTO;
        }
        // 记录表表信息
        HrmRsLeaveEntity hrmRsLeaveEntity = JSONObject.parseObject(after.getInfo(), HrmRsLeaveEntity.class);
        hrmRsLeaveEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmRsLeaveEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmRsLeaveEntity);

        return resultDTO;
    }

}